﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StuManModels;
using StuManDAL;

namespace StudentManageWinForm
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            _service = new UserService();
        }
        private UserService _service = null;
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!txtBoxUserName.Text.Any())
            {
                lbInfo.Text = "用户名不能为空";
                return;
            }
            if (!int.TryParse(txtBoxUserName.Text,out int userId))
            {
                lbInfo.Text = "用户名输入格式有误(应为纯数字)";
                return;
            }
            if (!txtBoxPassword.Text.Any())
            {
                lbInfo.Text = "密码不能为空";
                return;
            }
            SysUser user = new SysUser()
            {
                UserId = userId,
                UserPassword = txtBoxPassword.Text.Trim()
            };
            if (_service.VerifyUser(user)!=null)
            {
                Program.CurrentUser = user;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                lbInfo.Text = "登录失败,用户名或密码错误";
            }            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
